
* Load dataset
use "processed/RPImicro.dta", clear
keep if Irpi > 0
qui forvalues x = 1/17	 {
keep if s_RPI_`x' <1 
keep if s_RPI_`x' > -0.00001 
}

sort datayear Irpi

 
* Create list of unique years from datayear
levelsof datayear, local(years)

* Generate a unique group_id for each year in the dataset
egen group_id = group(datayear)

* Get the maximum group_id
local max_group_id = `: word count `years''

* Create a percentile variable (initialize with missing values)
gen pct10 = . 

* Loop through each unique year and calculate percentiles
forvalues i = 1 / `max_group_id' {
    * Calculate 100 quantiles for each year
    xtile temp_group = Irpi if group_id == `i', nq(10)
    
    * Update the percentile variable
    replace pct10 = temp_group if group_id == `i'
    
    * Drop temporary variable
    drop temp_group
}

* Drop unnecessary variable
drop group_id

save "processed/RPImicro10.dta",replace

use "processed/RPImicro10.dta",clear 

* Create list of unique years from datayear
levelsof datayear, local(years)

* Generate a unique group_id for each year in the dataset
egen group_id = group(datayear)

* Get the maximum group_id
local max_group_id = `: word count `years''

* Create a percentile variable (initialize with missing values)
gen pct100 = . 

* Loop through each unique year and calculate percentiles
forvalues i = 1 / `max_group_id' {
    * Calculate 100 quantiles for each year
    xtile temp_group = Irpi if group_id == `i', nq(100)
    
    * Update the percentile variable
    replace pct100 = temp_group if group_id == `i'
    
    * Drop temporary variable
    drop temp_group
}

* Drop unnecessary variable
drop group_id

save "processed/RPImicro100.dta",replace


use "processed/RPImicro100.dta",clear 

* Create list of unique years from datayear
levelsof datayear, local(years)

* Generate a unique group_id for each year in the dataset
egen group_id = group(datayear)

* Get the maximum group_id
local max_group_id = `: word count `years''

* Create a percentile variable (initialize with missing values)
gen pct1000 = . 

* Loop through each unique year and calculate percentiles
forvalues i = 1 / `max_group_id' {
    * Calculate 100 quantiles for each year
    xtile temp_group = Irpi if group_id == `i', nq(1000)
    
    * Update the percentile variable
    replace pct1000 = temp_group if group_id == `i'
    
    * Drop temporary variable
    drop temp_group
}

* Drop unnecessary variable
drop group_id

save "processed/RPImicro1000.dta",replace

use "processed/RPImicro1000.dta",clear 



* Create list of unique years from datayear
levelsof datayear, local(years)

* Generate a unique group_id for each year in the dataset
egen group_id = group(datayear)

* Get the maximum group_id
local max_group_id = `: word count `years''

* Create a percentile variable (initialize with missing values)
gen pct4 = . 

* Loop through each unique year and calculate percentiles
forvalues i = 1 / `max_group_id' {
    * Calculate 100 quantiles for each year
    xtile temp_group = Irpi if group_id == `i', nq(4)
    
    * Update the percentile variable
    replace pct4 = temp_group if group_id == `i'
    
    * Drop temporary variable
    drop temp_group
}

* Drop unnecessary variable
drop group_id

save "processed/RPImicro_master.dta",replace

use "processed/RPImicro_master.dta",clear 

bysort datayear pct1000: egen totexp_mean = mean(Irpi)
bysort datayear pct1000: egen totexp_median = median(Irpi)

forvalues i = 1 / 17 {
    * Calculate 100 quantiles for each year
    g exp_RPI_`i' = s_RPI_`i'*Irpi
bysort datayear pct1000: egen sumexp`i' =mean(exp_RPI_`i')
    g b_`i' = sumexp`i'/totexp_mean
}


duplicates drop datayear pct1000,force

keep b* datayear totexp_mean totexp_median pct*
*drop pct


merge m:1 datayear using processed/pricemerge_dis.dta
rename datayear year
drop _merge
xtset pct1000 year 
*rename price* p_*
order year pct1000 pct100 pct10 pct4
save "processed/UKquasi_panel.dta",replace

*rename year datayear 
